package cn.edu.nju.askme.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Component;

import cn.edu.nju.askme.model.Option;
import cn.edu.nju.askme.model.Paper;
import cn.edu.nju.askme.model.Question;
import cn.edu.nju.askme.service.IOptionService;
import cn.edu.nju.askme.service.IPaperService;
import cn.edu.nju.askme.service.IQuestionService;

import com.opensymphony.xwork2.ActionSupport;

@Component
public class QuestionAddAction extends ActionSupport implements SessionAware {

	//获取
	private Question question;
	private List<Option> optionList;
	private int pid;

	private Map session;
	private IQuestionService questionService;
	private IOptionService optionService;
	private IPaperService paperService;

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public List<Option> getOptionList() {
		return optionList;
	}

	public void setOptionList(List<Option> optionList) {
		this.optionList = optionList;
	}
	
	

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public IQuestionService getQuestionService() {
		return questionService;
	}

	@Resource
	public void setQuestionService(IQuestionService questionService) {
		this.questionService = questionService;
	}

	public IOptionService getOptionService() {
		return optionService;
	}

	@Resource
	public void setOptionService(IOptionService optionService) {
		this.optionService = optionService;
	}
	
	public IPaperService getPaperService() {
		return paperService;
	}
	@Resource
	public void setPaperService(IPaperService paperService) {
		this.paperService = paperService;
	}

	public String execute() {
		for (int i =0 ; i<optionList.size();i++) {
			Option op = optionList.get(i);
			if (op!=null) {
				op.setLabel(i+"");
				op.setQuestion(question);
			}
		}
		Paper p = paperService.checkById(pid);
		question.setPaper(p);
		question.setOptions(optionList);
		questionService.add(question);
		
		for (Option op :optionList) {
			if(op!=null&&op.getContent()!=null&&!op.getContent().equals(""))
				optionService.add(op);
		}
		
		return SUCCESS;
	}
}
